package org.dtrd.modules.food.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.common.system.vo.LoginUser;
import org.dtrd.modules.food.entity.bean.FoodMatchingDetail;
import org.dtrd.modules.food.entity.bean.FoodMatchingListInfo;
import org.dtrd.modules.food.entity.po.DtrdExtEmsYwFoodMatching;
import org.dtrd.modules.food.entity.request.FoodMatchingPageRequest;
import org.dtrd.modules.food.mapper.DtrdExtEmsYwFoodMatchingMapper;
import org.dtrd.modules.food.service.IDtrdExtEmsYwFoodMatchingService;
import org.dtrd.modules.util.ShiroUtil;
import org.dtrd.common.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;


/**
 * <p>
 * 配餐:早午晚三餐 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-09
 */
@DS("multi-datasource1")
@Service
public class DtrdExtEmsYwFoodMatchingServiceImpl extends ServiceImpl<DtrdExtEmsYwFoodMatchingMapper, DtrdExtEmsYwFoodMatching> implements IDtrdExtEmsYwFoodMatchingService {

    private DtrdExtEmsYwFoodMatchingMapper dtrdExtEmsYwFoodMatchingMapper;

    @Override
    public IPage<FoodMatchingListInfo> pageFoodMatching(FoodMatchingPageRequest request) {
        LambdaQueryWrapper<DtrdExtEmsYwFoodMatching> queryWrapper = baseQueryWrapper();
        String foodMatchingName = request.getFoodMatchingName();
        if (StrUtil.isNotBlank(foodMatchingName)) {
            queryWrapper.like(DtrdExtEmsYwFoodMatching::getMatchingName, StringUtil.fuzzySearchWrapper(foodMatchingName));
        }
        Integer foodMatchingCategoryId = request.getFoodMatchingCategoryId();
        if (foodMatchingCategoryId != null) {
            queryWrapper.eq(DtrdExtEmsYwFoodMatching::getMatchingCategoryid, foodMatchingCategoryId);
        }
        IPage<DtrdExtEmsYwFoodMatching> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(FoodMatchingListInfo::new);
    }

    @Override
    public FoodMatchingDetail getFoodMatching(Integer dataId) {
        DtrdExtEmsYwFoodMatching dtrdExtEmsYwFoodMatching = getById(dataId);
        return Optional.ofNullable(dtrdExtEmsYwFoodMatching)
                        .map(po -> new FoodMatchingDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateFoodMatching(FoodMatchingDetail dto) {
        DtrdExtEmsYwFoodMatching dtrdExtEmsYwFoodMatching = new DtrdExtEmsYwFoodMatching();
        LoginUser loginUser = ShiroUtil.getLoginUser();
        if(dto.getDataId() == null){
            dtrdExtEmsYwFoodMatching = dtrdExtEmsYwFoodMatching.init();
            if (loginUser != null) {
                dtrdExtEmsYwFoodMatching.setMatchingCreater(loginUser.getId());
            }
        }else {
            if (loginUser != null) {
                dtrdExtEmsYwFoodMatching.setMatchingEditor(loginUser.getId());
                dtrdExtEmsYwFoodMatching.setMatchingEditTime(new Date());
            }
        }
        dtrdExtEmsYwFoodMatching.parseFromDto(dto);
        return saveOrUpdate(dtrdExtEmsYwFoodMatching);
    }

    @Override
    public boolean removeFoodMatching(Integer dataId) {
        DtrdExtEmsYwFoodMatching query = this.getById(dataId);
        if (query == null) {
            return false;
        }
        LoginUser loginUser = ShiroUtil.getLoginUser();
        if (loginUser != null) {
            query.setMatchingDeleter(loginUser.getId());
            query.setMatchingDelTime(new Date());
            query.setIsDel(1);
        }
        return updateById(query);
    }

    private LambdaQueryWrapper<DtrdExtEmsYwFoodMatching> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdExtEmsYwFoodMatching.class)
                        .eq(DtrdExtEmsYwFoodMatching::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdExtEmsYwFoodMatching> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdExtEmsYwFoodMatching.class)
                        .eq(DtrdExtEmsYwFoodMatching::getIsDel, 0);
    }
}
